package com.cictec.middleware.core.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.core.entity.DeparterSchedule;
import com.cictec.middleware.core.service.ScheduleService;

@RestController
public class BusScheduleController {

	private static Logger logger = LoggerFactory.getLogger(ScheduleService.class);
	@Autowired
	private ScheduleService scheduleService;

	/**
	 * 根据条件，查询当天发车时刻
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/dc/busSchedule")
	public List<DeparterSchedule> selectBusRunScheduleDayList(@RequestBody Map params) {
		List<DeparterSchedule> list = scheduleService.selectBusRunScheduleDayList(params);
		return list;
	}

	/**
	 * 更新指定日期、线路、车辆的司机信息
	 * 
	 * @param date
	 *            签到时间
	 * @param lineUuid
	 * @param busUuid
	 * @param drvUuid
	 */
	@RequestMapping(value = "/dc/busSchedule/updateScheduleDriver")
	public void updateScheduleDriver(@RequestParam("signTime") Date signTime, @RequestParam("lineUuid") String lineUuid,
			@RequestParam("busUuid") String busUuid, @RequestParam("drvUuid") String drvUuid) {
		
		scheduleService.updateScheduleDriver(signTime, lineUuid, busUuid, drvUuid);
	}

	/**
	 * 【数据库】更新运营时刻表初始化（中间件为做操作前的状态）
	 * 
	 * @param date
	 * @param lineUuid
	 * @param busUuid
	 * @param departureFlag
	 */
	@RequestMapping(value = "/dc/busSchedule/updateScheduleDepartureFlag")
	public void updateScheduleDepartureFlag(@RequestParam("date") String date,
			@RequestParam("busUuid") String busUuid) {
		scheduleService.updateScheduleDepartureFlag(date, busUuid);
	}

	@RequestMapping(value = "/dc/busSchedule/updateBusSchedule")
	public void updateBusSchedule(@RequestBody Map params) {
		logger.debug("根据运营时刻表趟次信息");

		String lineUuid = PMSUtils.isNull(params.get("lineUuid"));
		String lineType = PMSUtils.isNull(params.get("lineType"));
		String busUuid = PMSUtils.isNull(params.get("busUuid"));
		String drvUuid = PMSUtils.isNull(params.get("drvUuid"));
		Date actStartTime = DateUtils.parseDateTime(PMSUtils.isNull(params.get("actStartTime")));
		Date actArraveTime = DateUtils.parseDateTime(PMSUtils.isNull(params.get("actArraveTime")));
		scheduleService.updateBusSchedule(lineUuid, lineType, busUuid, drvUuid, actStartTime, actArraveTime);
	}

	/**
	 * 更新指定趟次实际开始（结束时间）
	 * 
	 * @param brsdUuid
	 * @param startTime
	 * @param endTime
	 */
	@RequestMapping(value = "/dc/busSchedule/updateScheduleTripStartOrEndTime")
	public void updateScheduleTripStartOrEndTime(@RequestParam("brsdUuid") String brsdUuid,
			@RequestParam("startTime") Date startTime, @RequestParam("endTime") Date endTime) {
		scheduleService.updateScheduleTripStartOrEndTime(brsdUuid, startTime, endTime);
	}

}
